﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Net;
using System.IO;

namespace Chapter26.BasicWebClient
{
    /// <summary>
    /// WebHeaderWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WebHeaderWindow : Window
    {
        public WebHeaderWindow()
        {
            InitializeComponent();

            GetData();
        }

        private void GetData()
        {
            var hao360WebRequest = WebRequest.Create("https://hao.360.cn");
            var hao360HttpWebRequest = hao360WebRequest as HttpWebRequest;
            headerListBox.Items.Add("Request Timeout (ms) = " + hao360WebRequest.Timeout);
            headerListBox.Items.Add("Request Keep Alive = " + hao360HttpWebRequest.KeepAlive);
            headerListBox.Items.Add("Request AllowAutoRedirect = " + hao360HttpWebRequest.AllowAutoRedirect);
            var hao360WebResponse = hao360HttpWebRequest.GetResponse();
            var hao360Headers = hao360WebResponse.Headers;
            for (int i = 0; i < hao360Headers.Count; i++)
            {
                headerListBox.Items.Add(string.Format("Header {0}: {1}", hao360Headers.GetKey(i), hao360Headers[i]));
            }
        }

        private void GetMethodData()
        {
            var hao360WebRequest = WebRequest.Create("http://hao.360.cn");
            var hao360HttpWebRequest = hao360WebRequest as HttpWebRequest;
            var hao360WebResponse = hao360HttpWebRequest.GetResponse();
            using (var hao360DataStreamReader = new StreamReader(hao360WebResponse.GetResponseStream()))
            {
                headerListBox.Items.Add(hao360DataStreamReader.ReadToEnd());
            }
        }
    }
}
